/*
 * Copyright 2009 Bart Guijt and others.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.code.gwt.database.client.service.callback.rowid;

import java.util.ArrayList;
import java.util.List;

import com.google.code.gwt.database.client.service.RowIdListCallback;
import com.google.code.gwt.database.client.service.callback.DataServiceTransactionCallback;
import com.google.code.gwt.database.rebind.DataServiceGenerator;

/**
 * Used in the {@link DataServiceGenerator} to reduce generated boilerplate
 * code.
 * 
 * <p>
 * The Generator implements the onTransactionStart() method body.
 * </p>
 * 
 * @author bguijt
 */
public abstract class TransactionCallbackRowIdListCallback extends
    DataServiceTransactionCallback<RowIdListCallback> {

  private List<Integer> rowIds = new ArrayList<Integer>();

  /**
   * Creates a new TransactionCallback with the specified DataService' List
   * callback.
   */
  public TransactionCallbackRowIdListCallback(RowIdListCallback callback) {
    super(callback);
  }

  /**
   * Store the resultSet for later retrieval when the transaction has ended.
   */
  protected void addRowId(Integer rowId) {
    rowIds.add(rowId);
  }

  /**
   * Invokes the DataService' {@link RowIdListCallback#onSuccess(List)} callback
   * method with the List accumulated at {@link #addRowId(Integer)}.
   */
  public void onTransactionSuccess() {
    getCallback().onSuccess(rowIds);
  }
}
